#!/usr/bin/python
# -*- coding: utf8 -*-

import codecs, os

import httplib, urllib2
from chardet import detect

#from models import Movie
#from django.db import models

class Database_inspector(object):
   def __init__(self):
      pass

   def Missing_titles(self):
      pass

class HTTPCache(object):
   def __init__(self, cache_path = u'cache/'):
      self.cache_path = cache_path

   def Filename(self, url):
      url_info = self.url_split(url)
      filename = self.cache_path + u'_'.join([url_info['host']]+\
                 filter(len, url_info['path'].split(u'/')))
      return filename

   def Get(self, url):
      filename = self.Filename(url)
      if os.path.exists(filename) and os.path.isfile(filename):
         try:
            content = codecs.open(filename, "r", "utf8").read()
            return content
         except UnicodeDecodeError:
            pass
         content = open(filename, 'rb').read()
         return content
      else:
         return None

   def Add(self, content, url):
      filename = self.Filename(url)
      encoding = detect(content)['encoding']
      if encoding:
         content = content.decode(encoding, 'replace')
         codecs.open(filename, "w", "utf8").write(content)
      else:
         open(filename, 'wb').write(content)

   def __getitem__(self, url):
      return self.Get(url)

   def __setitem__(self, url, content):
      return self.Add(content, url)

   def url_split(self, url):
      data = {}
      protocol = url.find("://")
      if protocol == -1:
         protocol = 0
      else:
         protocol+= 3
      data["host"] = url[protocol:url.find("/", protocol)]
      params = url.find('?')
      if params == -1:
         data["path"]   = url[url.find("/", protocol):]
         data["params"] = ''
      else:
         data["path"] = url[url.find("/", protocol):url.find("?")]
         data["params"] = url[url.find("?")+1:]
      if url.endswith("/"):
         data["fullfilename"] = ''
         data["filename"]     = ''
         data["extension"]    = ''
      else:
         data["fullfilename"] = url[url.rfind("/")+1:]
         data["filename"]     = url[url.rfind("/")+1: url.rfind(".")]
         data["extension"]    = url[url.rfind(".")+1:]
      return data


class Web_parser(object):
   def __init__(self):
      self.cache = HTTPCache()

   def url_split(self, url):
      data = {}
      if url.find("://") == -1:
         protocol = 0
      else:
         protocol = url.find("://")+3
      data["host"] = url[protocol:url.find("/")]
      params = url.find('?')
      if params == -1:
         data["path"]   = url[url.find("/", protocol):]
         data["params"] = ''
      else:
         data["path"] = url[url.find("/", protocol):url.find("?")]
         data["params"] = url[url.find("?")+1:]
      if url.endswith("/"):
         data["fullfilename"] = ''
         data["filename"]     = ''
         data["extension"]    = ''
      else:
         data["fullfilename"] = url[url.rfind("/")+1:]
         data["filename"]     = url[url.rfind("/")+1: url.rfind(".")]
         data["extension"]    = url[url.rfind(".")+1:]
      return data

   def Open_url(self, url, request=None, opener=None):
      if not request:
         headers = {'User-Agent':
                    'Opera/9.63 (Windows NT 5.1; U; en-GB) Presto/2.1.1' }
         data    = None
         request = urllib2.Request(url, data, headers)
      if not opener:
         opener  = urllib2.build_opener()
      for i in range(15):
         try:
            handle = opener.open(request)
         except urllib2.URLError, e:
            if i == 14:
               print e
            pass
         else:
            return handle
      else:
         print "URLError opening", url
         return None

   def Download(self, handle):
      try:
         content = handle.read()
         return content
      except urllib2.HTTPError, e:
         print "HTTPError opening", url
         print e
         return None

   def Retrieve(self, url, binary=False):
      content = self.cache[url]
      if not content:
         handle = self.Open_url(url)
         if handle:
            print u'Downloading', url
            content = self.Download(handle)
            if content:
               self.cache[url] = content
      return content
